package com.fare.fdc.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.plane.channel.SourceApplication;
import com.fare.taluo.application.plane.city.PortApplication;
import com.fare.taluo.application.plane.flight.FlightInfoApplication;
import com.fare.taluo.core.domain.config.channel.SourceEntity;
import com.fare.taluo.core.domain.config.channel.SourceEntityRepository;
import com.fare.taluo.core.domain.config.channel.impl.SourceEntityRepositoryImpl;
import com.fare.taluo.util.JsonUtil;
import com.fare.taluo.util.StringUtils;
import com.fare.taluo.vo.core.FlightInfoVO;
import com.fare.taluo.vo.core.SourceEntityVO;
import com.fare.util.DateUtils;

@Controller("flightInfoQueryAction")
public class FlightInfoQueryAction extends BaseFdcAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6137412262940190492L;
	private List<SourceEntityVO> sourceLists;
	@Resource(name = "sourceApplication")
	private SourceApplication sourceApplication;
	@Resource(name = "portApplication")
	private PortApplication portApplication;
	@Resource(name = "flightInfoApplication")
	private FlightInfoApplication flightInfoApplication;
	private SourceEntityRepository sourceEntityRepository = new SourceEntityRepositoryImpl();
	private String companyCode;
	private String fromCityName;
	private String fromCityCode;
	private String toCityName;
	private String toCityCode;
	private String entryTime;
	private String flightNo;

	public String getFromCityCode() {
		return fromCityCode;
	}

	public void setFromCityCode(String fromCityCode) {
		this.fromCityCode = fromCityCode;
	}

	public String getToCityCode() {
		return toCityCode;
	}

	public void setToCityCode(String toCityCode) {
		this.toCityCode = toCityCode;
	}


	public String toAnalyseJSP() throws Exception {
		// sourceLists =
		// sourceApplication.findSourceEntityListByConditions("","","","",1,1000);
		sourceLists = sourceApplication.findCrawlAirCompanys();
		returnPageURL = "/fare/flightInfo.jsp";
		return "dispatcher";
	}

	public void analyse() throws JsonGenerationException, JsonMappingException,
			IOException {
		Date time = null;
		String flightNum = null;
		if (StringUtils.isNotEmpty(entryTime)) {
			time = DateUtils.strToDate(entryTime, "yyyy-MM-dd");
		}
		if (StringUtils.isNotEmpty(flightNo)) {
			flightNum = flightNo;
		}

		 List<FlightInfoVO> list = flightInfoApplication.findByConditions(
		 this.companyCode, fromCityCode, toCityCode, flightNum, time);
		Set<String> carrierNames = new HashSet<String>();
		// 查找所有航空公司
		for (FlightInfoVO flightInfoVO : list) {
			carrierNames.add(flightInfoVO.getCarrierName());
		}
		// 封装到Map
		Map<String, List<FlightInfoVO>> flights = new HashMap<String, List<FlightInfoVO>>();
		Iterator<String> it = carrierNames.iterator();
		while (it.hasNext()) {
			String carrierName = it.next();
			List<FlightInfoVO> flightInfoVO = new ArrayList<FlightInfoVO>();
			for (FlightInfoVO l : list) {
				if (l.getCarrierName().equals(carrierName))
					flightInfoVO.add(l);
			}
			flights.put(carrierName, flightInfoVO);
		}

		 String data = this.toJSON(list, list.size());
//		String data = this.toJSON(flights, flights.size());
		super.toJSONOut(data);

	}

	/**
	 * 
	 * 传入obj取得JSON
	 * 
	 * @param resultList
	 * @param total
	 * @return
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public String toJSON(Object obj, int total) throws JsonGenerationException,
			JsonMappingException, IOException {
		String jsonStr = "{\"Total\":" + total + "," + "\"Rows\":";
		int i = 0;
		String json = JsonUtil.toJson(obj);
		jsonStr += json;
		jsonStr += "}";
		return jsonStr;
	}

	private String getSourceEntityHbKeyByName(String name) {
		String key = null;
		try {
			SourceEntity se = sourceEntityRepository
					.getSourceEntityByName(name);
			if (se != null) {
				// System.out.println(se.getHbaseKey());
				key = se.getHbaseKey();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return key;
	}

	public List<SourceEntityVO> getSourceLists() {
		return sourceLists;
	}

	public void setSourceLists(List<SourceEntityVO> sourceLists) {
		this.sourceLists = sourceLists;
	}

	public String getCompanyCode() {
		return companyCode;
	}

	public void setCompanyCode(String companyCode) {
		this.companyCode = companyCode;
	}

	public String getFromCityName() {
		return fromCityName;
	}

	public void setFromCityName(String fromCityName) {
		this.fromCityName = fromCityName;
	}

	public String getToCityName() {
		return toCityName;
	}

	public void setToCityName(String toCityName) {
		this.toCityName = toCityName;
	}

	public String getEntryTime() {
		return entryTime;
	}

	public void setEntryTime(String entryTime) {
		this.entryTime = entryTime;
	}

	public SourceApplication getSourceApplication() {
		return sourceApplication;
	}

	public void setSourceApplication(SourceApplication sourceApplication) {
		this.sourceApplication = sourceApplication;
	}

	public PortApplication getPortApplication() {
		return portApplication;
	}

	public void setPortApplication(PortApplication portApplication) {
		this.portApplication = portApplication;
	}

	public FlightInfoApplication getFlightInfoApplication() {
		return flightInfoApplication;
	}

	public void setFlightInfoApplication(
			FlightInfoApplication flightInfoApplication) {
		this.flightInfoApplication = flightInfoApplication;
	}

	public SourceEntityRepository getSourceEntityRepository() {
		return sourceEntityRepository;
	}

	public void setSourceEntityRepository(
			SourceEntityRepository sourceEntityRepository) {
		this.sourceEntityRepository = sourceEntityRepository;
	}

}
